<?php
class Shipping_Service_ShipTypeCountryMap
{
    public static function add($row){
        $obj = new Shipping_Model_ShipTypeCountryMap();
        return $obj->add($row);
	}

	public static function update($row,$value,$field='stcm_id'){
        $obj = new Shipping_Model_ShipTypeCountryMap();
        return $obj->update($row,$value,$field);
	}


	public static function delete($value,$field='stcm_id'){
        $obj = new Shipping_Model_ShipTypeCountryMap();
        return $obj->delete($value,$field);

	}


	public static function getByValue($value,$field='stcm_id'){
        $obj = new Shipping_Model_ShipTypeCountryMap();
        return $obj->getByValue($value,$field);
	}

    public static function getByCondition($condition,$type='*',$orderby="",$page=0,$pageSize=0){
        $obj = new Shipping_Model_ShipTypeCountryMap();
        return $obj->getByCondition($condition,$type,$orderby,$page,$pageSize);
	}

    public static function validator($val) {
        $lang = Zend_Registry::get('lang');
        $error = array();
        $validateArr = array();
        $validateArr[] = array('name' => $lang['shipping_method'],'value' => $val['st_code'],'regex' => array('require'));
        $validateArr[] = array('name' => $lang['country'], 'value' => $val['country_id'], 'regex' => array('require'));
        $validateArr[] = array('name' => $lang['area'].$lang['label'], 'value' => $val['stcm_area'], 'regex' => array('require'));
        $error = Common_Service_Validator::formValidator($validateArr);

        if(!empty($val['st_code'])){
            $condition = array(
                'st_code' =>$val['st_code'],
                'stcm_area' =>$val['stcm_area'],
                'country_id' =>$val['country_id']
            );
            $list = self::getByCondition($condition);
            if(!empty($list)){
                $error[] =$lang['shipping_method_exists'];
            }
        }
        return $error;

    }
    
    /**
     * 批量导入
     * @param string $fileName
     * @return array
     */
    public static function batchAddShipTypeCountryMap($shipType,$row){
    	$productSer = new Product_Service_Product();
    	$adapter = $productSer->getAdapter();
    	$adapter->beginTransaction();
    	try {    		
    		//删除物流方式对应的纪录
    		self::delete($shipType,'st_code');
    		$errlist = array();
    		foreach($row as $v){
    			$arr = array();
    			$arr['st_code'] = $v['ship_type'];
    			$arr['stcm_area'] = $v['stcm_area'];
    			$arr['country_code'] = $v['country_code'];
    		
    			$country =  Common_Service_Country::getByValue($v['country_code'],"country_code");
    			$arr['country_name'] = $country['cn_name'];
    			$arr['country_id'] = $country['country_id'];
    			//     		print_r($arr);//exit;
    			$validat = self::validator($arr);
    			if(empty($validat)){
    				self::add($arr);
    			}else{
    				$arr['err'] = $validat;
    				$errlist[] = $arr;
    			}
    		
    		}
    		
    		$adapter->commit();    		
    		return array('status'=>1,'msg'=>"success",'errlist'=>$errlist);
    	}catch (Exception $e){
    		$adapter->rollback();
    		return array('status'=>0,'msg'=>$e->getMessage());
    	}
    	
    
    }
    
}